<template>
  <el-card>
    <div class="changetel">
      <h2 class="applyRecode">修改手机号</h2>
      <el-form class="form" :model="form" :rules="rules" ref="form" label-width="110px">
        <el-form-item label="原密码：" prop="oldPasswd">
          <el-input placeholder="请输入原密码" v-model="form.oldPasswd"></el-input>
        </el-form-item>
        <el-form-item class="newphone" label="新手机号码：" prop="mobileNo">
          <el-input class="newphoneInp" placeholder="请输入手机号码" v-model="form.mobileNo"></el-input>
          <el-button class="code" @click="sendcode" type="primary" :disabled="disabled" size="small">{{codeText}}</el-button>
        </el-form-item>
        <el-form-item label="手机验证码：" prop="mobileCode">
          <el-input placeholder="请输入手机验证码" v-model="form.mobileCode"></el-input>
        </el-form-item>
      </el-form>
      <div class="btns">
        <el-button type="primary" size="small" @click="cancel">取消</el-button>
        <el-button type="primary" size="small" @click="init">确认</el-button>
      </div>
    </div>
  </el-card>
</template>
<script>
export default {
  name: 'changetel',
  data () {
    return {
      codeText: '发送验证码',
      disabled: false,
      form: {
        oldPasswd: '',
        mobileNo: '',
        mobileCode: ''
      },
      rules: {
        oldPasswd: [
          { required: true, message: '请输入原密码', trigger: 'blur' }
        ],
        mobileNo: [
          { required: true, message: '请填写新手机号！', trigger: 'blur' },
          { pattern: /^1[345678]\d{9}$/, message: '手机号格式不正确！', trigger: 'blur' }
        ],
        mobileCode: [
          { required: true, message: '请输入手机验证码', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
  },
  methods: {
    sendcode () {
      this.$tkAjax.post(`${this.$config.baseUrl}/user/merchantUser/sendMobileCode`, {}, {
        params: {
          userId: this.$store.state.userId,
          mobileNo: this.form.mobileNo,
          pageFlg: 3
        }
      }).then(res => {
        this.$message.success(res.data.content)
        this.disabled = true
        let num = 60
        let setinit = setInterval(() => {
          num--
          this.codeText = `${num}秒后重新获取`
          if (num === 0) {
            this.codeText = '发送验证码'
            this.disabled = false
            clearInterval(setinit)
          }
        }, 1000)
      }).catch(err => {
        this.$message.warning(err.data.message)
      })
    },
    init () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$tkAjax.post(`${this.$config.baseUrl}/user/merchantUser/resetMerchantMobile`, {}, {
            params: {
              userId: this.$store.state.userId,
              oldPasswd: this.$md5(this.form.oldPasswd),
              mobileNo: this.form.mobileNo,
              mobileCode: this.form.mobileCode
            }
          }).then(res => {
            this.$message.success(res.data.content)
          }).catch(err => {
            this.$message.warning(err.data.message)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    cancel () {
      this.$router.push('/')
    }
  }
}
</script>
<style lang="scss" scoped>
.changetel {
  .form {
    width: 50%;
    color: #000;
    margin: 0 auto;
    .newphone {
      .newphoneInp {
        width: 62%;
        margin-right: 20%;
      }
    }
  }
  .btns {
    // width: 50%;
    text-align: center;
    margin: 42px 0 14px;
  }
}
</style>
